/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package as24gc.common;

import as24gc.db.Vehicles;
import java.awt.image.BufferedImage;
import java.io.File;
import java.net.URL;
import java.util.Date;
import javax.imageio.ImageIO;

/**
 *
 * @author nico.vassallo
 */
public class Utility {

    public static final String PATH_APP = "files/as24gc/";
    public static final String PATH_SESSION = PATH_APP+"sessionsfiles/";
    public static final String CSV_FILENAME = "csvfile.csv";

    public static String generateSessionId() {
        String sid = "" + new Date().getTime();

         // Create PATH_SESSION directory
        if (!new File(PATH_SESSION).exists()) {
            new File(PATH_SESSION).mkdir();
        }
        
        // Create PATH_SESSION/SESSION directory
        if (!new File(PATH_SESSION + sid).mkdir()) {
            sid = null;
        }

        System.out.println("***SID: " + sid);
        return sid;
    }

    public static void getAndSaveImage(Vehicles v, String path) throws Exception {
        String[] mediaArray = v.getMedia().split(";");
        String timestamp = ""+new Date().getTime();
        String tmpNewMedia = "";

        for (int i = 0; i < mediaArray.length; i++) {
            mediaArray[i] = mediaArray[i].trim();

            String filename = getImageFileName(mediaArray[i]);

            String ext = filename.substring(filename.lastIndexOf(".") + 1, filename.length());
            System.out.println("ext: " + ext);

            mediaArray[i] = mediaArray[i].replaceAll(" ", "%20");
            System.out.println("mediaArray[" + i + "]: " + mediaArray[i]);

            // Create a URL for the image's location
            URL url = new URL(mediaArray[i]);

            BufferedImage bi = ImageIO.read(url);
            System.out.println("path+filename: " + path + filename);
            File outputfile = new File(path + filename);
            System.out.println("ImageIO.write: " + ImageIO.write(bi, ext, outputfile));
            
            String renamedFileName = "";
            if ((i+1) < 10) {
                renamedFileName = timestamp+"_0"+(i+1)+"."+ext;
            }
            else {
                renamedFileName = timestamp+"_"+(i+1)+"."+ext;
            }
            System.out.println("renamedFileName: " + path + renamedFileName);
            
            outputfile.renameTo(new File(path + renamedFileName));
            tmpNewMedia += renamedFileName;
            if ((i+1) != mediaArray.length) {
                tmpNewMedia += ";";
            }
            outputfile.delete();
        }
         System.out.println("NewMedia: "+tmpNewMedia);
        v.setMedia(tmpNewMedia);;
    }

    public static String getImageFileName(String url) {
        int indexLastDelimiter = url.lastIndexOf("/") + 1;
        String filename = url.substring(indexLastDelimiter, url.length());
        System.out.println("filename: " + filename);
        return filename;
    }
}
